//
// Created by benxb on 2022/3/6.
//

#include <filesystem>
#include <or_reader.h>
#include <kitti_reader.h>
#include <ns_reader.h>
#include <nsh_reader.h>
#include "data_reader.h"

DataReader::DataReader(const std::string& data_dir) {
    data_dir_ = data_dir + "/data/";
    for (const auto & entry : std::filesystem::directory_iterator(data_dir_))
        data_files_.push_back(entry.path());
    next_index = 0;
    timestamp_file_.open(data_dir + "timestamps.txt", std::ios::in);
}

bool DataReader::HasMoreSweep() {
    return next_index < data_files_.size();
}

DataReader *DataReader::GetReader(const std::string& reader_name, const std::string& data_dir) {
    if (reader_name == "OrReader") {
        return new ORReader(data_dir);
    } else if (reader_name == "KittiReader") {
        return new KittiReader(data_dir);
    } else if (reader_name == "NsReader") {
        return new NSReader(data_dir);
    } else if (reader_name == "NSHReader") {
        return new NSHReader(data_dir);
    }
    return nullptr;
}

long long int DataReader::ReadTimestamp() {
    std::string line;
    getline(timestamp_file_, line);
    return stoll(line);
}
